package com.itfreer.print.toPdf;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.commons.io.IOUtils;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * 定义将image转换为pdf接口
 */
public class ImageToPdfImp {

	/**
	 * 定义将image转换为pdf
	 * @param images
	 * @param pdfFile
	 * @throws DocumentException
	 * @throws IOException
	 */
	public void toPdf(List<InputStream> images, OutputStream pdfFile) throws DocumentException, IOException {

		// step 1: creation of a document-object
		Document document = new Document();

		// step 2:
		PdfWriter.getInstance(document, pdfFile);

		// step 3: we open the document
		document.open();

		for (InputStream image : images) {
			// step 4: we add content
			Image jpg = Image.getInstance(IOUtils.toByteArray(image));
			jpg.scalePercent(100);
			document.add(jpg);
		}

		document.close();
	}
}
